package qin.view;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.regex.Pattern;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

import qin.BookInformationSystem;
import qin.entity.Book;
import qin.entity.R;
import qin.service.BookService;
import qin.service.Impl.BookServiceImpl;

/**
 * 图书信息录入界面
 * @author 小行星
 *
 */
public class BookInputView {
	private BookInformationSystem bookInformationSystem;
	private BookService bookService;
	private JFrame frame;
	private JTextField number;
	private JTextField name;
	private JTextField price;
	private JTextField author;
	private JTextField press;
	private JLabel numberLabel;
	private JLabel nameLabel;
	private JLabel priceLabel;
	private JLabel authorLabel;
	private JLabel pressLabel;
	private JButton edit;
	private JButton add;

	public BookInputView(BookInformationSystem bookInformationSystem) {
		this.bookService = BookServiceImpl.build();
		this.bookInformationSystem = bookInformationSystem;
		initialize();
	}

	private void initialize() {
		frame = new JFrame();
		frame.setBounds(100, 100, 450, 322);
//		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.getContentPane().setLayout(null);
		
		numberLabel = new JLabel("书号");
		numberLabel.setBounds(10, 29, 54, 15);
		frame.getContentPane().add(numberLabel);
		
		nameLabel = new JLabel("书名");
		nameLabel.setBounds(10, 72, 54, 15);
		frame.getContentPane().add(nameLabel);
		
		priceLabel = new JLabel("价格");
		priceLabel.setBounds(10, 150, 54, 15);
		frame.getContentPane().add(priceLabel);
		
		authorLabel = new JLabel("作者");
		authorLabel.setBounds(10, 113, 54, 15);
		frame.getContentPane().add(authorLabel);
		
		pressLabel = new JLabel("出版商");
		pressLabel.setBounds(10, 190, 54, 15);
		frame.getContentPane().add(pressLabel);
		
		number = new JTextField();
		number.setBounds(66, 26, 343, 21);
		frame.getContentPane().add(number);
		number.setColumns(10);
		
		name = new JTextField();
		name.setColumns(10);
		name.setBounds(66, 69, 343, 21);
		frame.getContentPane().add(name);
		
		author = new JTextField();
		author.setColumns(10);
		author.setBounds(66, 110, 343, 21);
		frame.getContentPane().add(author);
		
		price = new JTextField();
		price.setColumns(10);
		price.setBounds(66, 147, 343, 21);
		frame.getContentPane().add(price);
		
		press = new JTextField();
		press.setColumns(10);
		press.setBounds(66, 187, 343, 21);
		frame.getContentPane().add(press);
		
		edit = new JButton("修改");
		edit.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				Pattern pattern = Pattern.compile("^[-\\+]?\\d*[.]\\d+$");
				// 先保证价格是浮点数
				String priceText = price.getText().trim();
				if(!pattern.matcher(priceText).matches()) {
					price.setText("");
					return;
				}
				
				Book book = new Book();
				book.setNumber(number.getText());
				book.setName(name.getText());
				book.setAuthor(author.getText());
				book.setPrice(Double.valueOf(priceText));
				book.setPress(press.getText());
				
				R<?> r = bookService.updateBook(book.getNumber(), book);
				if(r.isStatus()) {
					bookInformationSystem.flushModel();
					// 关闭当前窗口
					frame.setVisible(false);
				}else {
					JOptionPane.showMessageDialog(null, "修改失败！"+r.getMsg(), "修改图书", 2);
				}
			}
		});
		edit.setBounds(123, 228, 213, 45);
		
		add = new JButton("添加");
		add.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				Pattern pattern = Pattern.compile("^[-\\+]?\\d*[.]\\d+$");
				// 先保证价格是浮点数
				String priceText = price.getText().trim();
				if(!pattern.matcher(priceText).matches()) {
					price.setText("");
					return;
				}
				
				Book book = new Book();
				book.setNumber(number.getText());
				book.setName(name.getText());
				book.setAuthor(author.getText());
				book.setPrice(Double.valueOf(priceText));
				book.setPress(press.getText());
				
				R<?> r = bookService.addBook(book);
				if(r.isStatus()) {
					bookInformationSystem.flushModel();
					// 关闭当前窗口
					frame.setVisible(false);
				}else {
					JOptionPane.showMessageDialog(null, "添加失败！"+r.getMsg(), "修改图书", 2);
				}
			}
		});
		add.setBounds(123, 228, 213, 45);
	}
	
	public void showEdit(Book book) {
		frame.setTitle("编辑");
		edit.setText("修改");
		frame.getContentPane().add(edit);
		
		number.setText(book.getNumber());
		name.setText(book.getName());
		author.setText(book.getAuthor());
		price.setText(book.getPrice().toString());
		press.setText(book.getPress());
		
		frame.setVisible(true);
	}
	
	public void showAdd() {
		frame.setTitle("新增");
		add.setText("添加");
		frame.getContentPane().add(add);
		frame.setVisible(true);
	}
}
